package com.ccc.boot.security.controller;

import com.ccc.boot.exception.BusinessException;
import com.ccc.boot.response.ResponseCode;
import com.ccc.boot.response.ResponseResult;
import com.ccc.boot.security.constant.AuthConstant;
import com.ccc.boot.security.pojo.AuthUserData;
import com.ccc.boot.security.service.ResourceService;
import com.ccc.boot.security.util.AuthUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 资源——REST接口
 *
 * @author superC
 * @version 0.1.0
 * @date 2022/3/25
 */
@RestController
@RequestMapping("/resource")
public class ResourceController {

    protected Logger logger = LoggerFactory.getLogger(ResourceController.class);

    @Autowired
    private AuthUtils authUtils;

    @Autowired
    private ResourceService resourceService;

    /**
     * 获取资源
     *
     * @return {@link ResponseResult}
     * @date 2022/4/12 16:24
     * @author superC
     */
    @GetMapping("/getResource")
    public ResponseResult getResource() {
        // 获取当前登录用户信息
        AuthUserData authUserData = authUtils.getLoginUser();
        // 获取资源
        return resourceService.getResource(authUserData);
    }

    @GetMapping("/getUsername")
    public ResponseResult getUsername() {
        // 获取token中的用户名
        String username = authUtils.getUsername();
        return ResponseResult.success(username);
    }

    /**
     * 刷新令牌
     *
     * @date 2022/4/11 15:06
     * @param token
     * @return {@link ResponseResult}
     * @author superC
     */
    @GetMapping("/refreshToken")
    public ResponseResult refreshToken(@RequestHeader(AuthConstant.TOKEN_HEADER) String token) {
        String newToken = authUtils.refreshToken(token);
        return ResponseResult.success("刷新令牌成功", newToken);
    }

}